﻿namespace Shortcuts.Views
{
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using Microsoft.Phone.Tasks;
    using Shortcuts.Common;
    using Shortcuts.ViewModels;
    using System;
    using System.Collections.Generic;
    using System.IO.IsolatedStorage;
    using System.Windows;
    using System.Windows.Navigation;

    public partial class Home : PhoneApplicationPage
    {
        HomePageContext Context { get { return DataContext as HomePageContext; } }

        #region buttons

        ApplicationBarIconButton[] InitializeButtons()
        {
            List<ApplicationBarIconButton> buttons = new List<ApplicationBarIconButton>();

            var button = new ApplicationBarIconButton();
            button.Text = "unpin".Localized();
            button.IconUri = new Uri("/Images/unpin.png", UriKind.Relative);
            button.Click += OnUnpinIconButtonClick;
            buttons.Add(button);

            button = new ApplicationBarIconButton();
            button.Text = "about".Localized();
            button.IconUri = new Uri("/Images/info.png", UriKind.Relative);
            button.Click += OnAboutMenuClick;
            buttons.Add(button);

            if (!IsolatedStorageSettings.ApplicationSettings.Contains("like"))
            {
                button = new ApplicationBarIconButton();
                button.Text = "like".Localized();
                button.IconUri = new Uri("/Images/like.png", UriKind.Relative);
                button.Click += OnLikeMenuClick;
                buttons.Add(button);
            }

            return buttons.ToArray();
        }

        #endregion

        public Home()
        {
            InitializeComponent();

            Loaded += OnPageLoaded;
        }

        void OnPageLoaded(object sender, RoutedEventArgs e)
        {
            DataContext = new HomePageContext();            
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ApplicationBar.Buttons.Clear();
            foreach (var button in InitializeButtons())
            {
                ApplicationBar.Buttons.Add(button);
            }
            
            // clear.
            SystemTray.SetProgressIndicator(this, null);

            base.OnNavigatedTo(e);
        }

        private void OnUnpinIconButtonClick(object sender, EventArgs e)
        {
            foreach (var tile in TileHelper.Tiles)
            {
                tile.Delete();
            }

            ProgressIndicator indicator = new ProgressIndicator();
            indicator.IsIndeterminate = false;
            indicator.Text = "unpin-tips".Localized();
            indicator.IsVisible = true;

            SystemTray.SetProgressIndicator(this, indicator);
        }

        private void OnAboutMenuClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/AboutPage.xaml", UriKind.Relative));
        }

        private void OnLikeMenuClick(object sender, EventArgs e)
        {
            IsolatedStorageSettings.ApplicationSettings.Add("like", "true");
            MarketplaceReviewTask review = new MarketplaceReviewTask();
            review.Show();
        }


    }
}